package com.jlsoft.config;

import com.baomidou.mybatisplus.enums.DBType;
import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.LogicSqlInjector;
import com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.jlsoft.config.plugins.CameHumpinterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.apache.ibatis.annotations.Mapper;
import javax.sql.DataSource;
import java.util.Properties;

/**
 * mybatis 配置文件
 */
@Configuration
@EnableTransactionManagement()
@MapperScan(basePackages = "com.jlsoft", markerInterface = BaseMapper.class)
public class MybatisConfig {

    private static final String basePackages = "com.jlsoft";

    private static final String mybatisFilePath = "classpath:mybatis-config.xml";

    private static final String mapppersFilePaths = "classpath*:mapper/**/*Mapper.xml";

    PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

    @Bean
    public SqlSessionFactory sessionFactory (DataSource dataSource) throws Exception {
        //使用mp的工厂类
        MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
        //SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        //数据源
        sessionFactoryBean.setDataSource(dataSource);
        //mybatis-config配置文件位置
        sessionFactoryBean.setConfigLocation(resourcePatternResolver.getResource(mybatisFilePath));
        //sessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources(mapppersFilePaths));

        sessionFactoryBean.setTypeEnumsPackage("com.jlsoft.eunus");

       sessionFactoryBean.setGlobalConfig(globalConfig());

        sessionFactoryBean.setPlugins(new Interceptor[]{
            cameHumpinterceptor (),
            new PaginationInterceptor(),
           // new PerformanceInterceptor(),
            new OptimisticLockerInterceptor()
        });

        return sessionFactoryBean.getObject();
    }

    /**
     * mp的全局策略配置文件
     * @return
     */
    @Bean
    public GlobalConfiguration globalConfig () {
        GlobalConfiguration globalConfig = new GlobalConfiguration();

        Properties properties = new Properties();

        //逻辑删除 定义下面3个参数
        LogicSqlInjector sqlInjector = new LogicSqlInjector();//逻辑删除Sql注入器
        globalConfig.setSqlInjector(sqlInjector);
        globalConfig.setLogicDeleteValue("-1");
        globalConfig.setLogicNotDeleteValue("-1");

        //全局ID类型
        globalConfig.setIdType(IdType.AUTO.getKey());

        //数据库类类型
        globalConfig.setDbType(DBType.MYSQL.getDb());

        //主键Sequence
        //globalConfig.setKeyGenerator();

        //公共字段填充处理器
        //globalConfig.setMetaObjectHandler();

        //数据库关键字转义符
        //globalConfig.setIdentifierQuote();

        //刷新mapper 调试神器
        globalConfig.setRefresh(true);

        //数据库大写下划线转换
        //globalConfig.setCapitalMode();


        return globalConfig;
    }

    /*@Bean
    public MapperScannerConfigurer mapperScannerConfigurer () {
        MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
        scannerConfigurer.setBasePackage(basePackages);
        scannerConfigurer.setAnnotationClass(Mapper.class);
        return scannerConfigurer;
    }*/

    @Bean
    public PlatformTransactionManager transactionManager (DataSource dataSource) {
        System.out.println(">>>>>>数据源:"+dataSource.toString());
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public Object testTransactionManager (PlatformTransactionManager transactionManager) {
        System.out.println(">>>>>>事务管理器:"+transactionManager.getClass().getName());
        return new Object();
    }

    @Bean
    public CameHumpinterceptor cameHumpinterceptor () {
        return new CameHumpinterceptor();
    }

}
